<?php

require_once CORE_DIR . '/system/db/index.php';

abstract class Model extends QDb
{
    function __construct($name = '')
    {
        
    }

    public static function Load($name)
    {
        return self::loadModel($name);
    }

    private static function loadModel($name)
    {
        $modelFile = strtolower("{$name}." . CORE_PHP_EXT);
        $paths = (array)QXC()->Config->models;

        if($modulePath = QXC::getGlobal("ModulePath", "QXC"))
        {
            array_unshift($paths, "{$modulePath}/models");
        }

        if($modelPath = QXC::pathExist($paths, $modelFile))
        {
            include_once ($modelPath);

            $mName = "{$name}Model";

            return new $mName();
        }

        throw new QException("Model '{$modelFile}' not found");
    }

    // Little more magic
    function __set($name, $value)
    {
        parent::SetVar($name, $value);
    }

    function __get($name)
    {
        if($ext = parent::loadExtension($name))
            return $ext;
        else
            return parent::GetVar($name);
    }
}
